package com.example.lishop.service.impl

import com.example.lishop.entity.BCompanyEntity
import com.example.lishop.exception.ExceptEnum
import com.example.lishop.repository.CompanyRepository
import com.example.lishop.service.CompanyService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.util.Assert
import java.util.*

/**
 * Created on 2021/3/14 18:14
 * @author shuguo
 */
@Service
class CompanyServiceImpl : CompanyService{

    @Autowired
    lateinit var repository: CompanyRepository

    override fun add(data: BCompanyEntity): BCompanyEntity {
        return repository.save(data)
    }

    override fun update(data: BCompanyEntity) {
        detail(data.companyId)
        return repository.update(data)
    }

    override fun del(companyId: String) {
        detail(companyId)
        repository.del(companyId)
    }

    override fun list(page:Int, size:Int): Page<BCompanyEntity> {
        val req: Pageable = PageRequest.of(page, size)
        return repository.findAll(req)
    }

    override fun detail(companyId: String): BCompanyEntity? {
        val data:BCompanyEntity? = repository.findByCompanyId(companyId)
        Assert.notNull(data, ExceptEnum.NOT_EXIST)
        return data
    }

}